package policy;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;

import java.util.List;

/**
 * 表格内设置超链接
 *
 * @author yousj
 * @since 2022/4/30
 */
@Data
@EqualsAndHashCode(callSuper = true)
@AllArgsConstructor
public class LoopRowTableHyperlinkRenderPolicy extends LoopRowTableRenderPolicy {

    /**
     * 列
     */
    private int index;

    /**
     * 超链接text key
     */
    private String column;

    /**
     * 超链接url key
     */
    private String hyperlinkColumn = "url";

    public LoopRowTableHyperlinkRenderPolicy(int index, String column) {
        this.index = index;
        this.column = column;
    }

    @Override
    protected void afterloop(XWPFTable table, Object data) {
        try {
            List<JSONObject> jsonObjectList = JSONArray.parseArray(JSON.toJSONString(data), JSONObject.class);
            List<XWPFTableRow> rows = table.getRows();
            // index从1开始, 跳过表头
            for (int i = 1; i < rows.size(); i++) {
                XWPFTableCell cell = rows.get(i).getCell(index);
                List<XWPFParagraph> paragraphs = cell.getParagraphs();
                for (XWPFParagraph paragraph : paragraphs) {
                    JSONObject js = jsonObjectList.get(i - 1);
                    String url = String.valueOf(js.get(hyperlinkColumn));
                    String value = String.valueOf(js.get(column));
                    if (StrUtil.isNotBlank(url)) {
                        // 链接不为空的内容设置超链接
                        setLink(url, value, paragraph);
                    } else {
                        // 链接为空的则不设置, 正常填充值
                        cell.setText(value);
                    }
                    // 居中
                    paragraph.setAlignment(ParagraphAlignment.CENTER);
                    paragraph.setVerticalAlignment(TextAlignment.CENTER);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private void setLink(String url, String text, XWPFParagraph e) {
        String id = e.getDocument().getPackagePart().addExternalRelationship(url,
                XWPFRelation.HYPERLINK.getRelation()).getId();
        CTHyperlink ctHyperlink = e.getCTP().addNewHyperlink();
        ctHyperlink.setId(id);
        CTText ctText = CTText.Factory.newInstance();
        // 设置值
        ctText.setStringValue(text);
        CTR ctr = CTR.Factory.newInstance();
        CTRPr rpr = ctr.addNewRPr();
        CTColor color = CTColor.Factory.newInstance();
        // 设置颜色
        color.setVal("0000FF");
        rpr.setColorArray(new CTColor[]{color});
        // 单下划线
        // rpr.addNewU().setVal(STUnderline.SINGLE);
        // 无下划线
        rpr.addNewU().setVal(STUnderline.NONE);
        ctr.setTArray(new CTText[]{ctText});
        ctHyperlink.setRArray(new CTR[]{ctr});
    }

}
